package com.joey.imagepickerdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.alp.scan.activity.CaptureActivity;
import com.alp.scan.common.PermissionSetting;
import com.alp.scan.util.RequestCode;
import com.joey.imagepickerdemo.scan.OnScanClickListener;
import com.joey.imagepickerdemo.scan.ScanEditText;

/**
 * Created by Joey on 2019/3/21.
 *
 * @author by Joey
 */

public class MainActivity extends AppCompatActivity {


    ScanEditText scanEditText;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scanEditText =  findViewById(R.id.tv_value);
        scanEditText.setOnScanClickListener(new OnScanClickListener() {
            @Override
            public void onClick(View view) {
                PermissionSetting.ShowScanWindows(MainActivity.this, RequestCode.REQ_CODE);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (data == null) {
            return;
        }
        if (requestCode == RequestCode.REQ_CODE) {
            String scanResult = data.getStringExtra(CaptureActivity.SCAN_QRCODE_RESULT);
            View view = this.getCurrentFocus();
            if (view == null) {
                return;
            }
            if (view instanceof ScanEditText) {
                ScanEditText etScan = (ScanEditText) view;
                etScan.setText(scanResult);
                etScan.setSelection(scanResult.length());
                if (etScan.getResultListener() != null) {
                    etScan.getResultListener().onResult(scanResult, etScan);
                }

            }

        }
    }



}